# Makefile for OpenResty Docker Management

# 配置变量
PROJECT_NAME := gate
IMAGE_NAME := $(PROJECT_NAME)
CONTAINER_NAME := $(PROJECT_NAME)
VERSION=$(shell git describe --tags --always)
PORT := 80
SSL_PORT := 443

# Docker镜像标签
IMAGE_TAG := $(IMAGE_NAME):$(VERSION)
LATEST_TAG := $(IMAGE_NAME):latest

# 路径配置
DOCKERFILE := Dockerfile
CONF_DIR := config
LUA_DIR := lua
HTML_DIR := html
LOGS_DIR := logs

.PHONY: help build rebuild run start stop restart clean logs shell test deploy

# 显示帮助信息
help:
	@echo "OpenResty Docker 管理命令:"
	@echo ""
	@echo "  make build      - 构建Docker镜像"
	@echo "  make rebuild    - 重新构建镜像（不使用缓存）"
	@echo "  make run        - 运行容器（前台模式）"
	@echo "  make start      - 启动容器（后台模式）"
	@echo "  make stop       - 停止容器"
	@echo "  make restart    - 重启容器"
	@echo "  make clean      - 停止并删除容器"
	@echo "  make clean-all  - 停止并删除容器和镜像"
	@echo "  make logs       - 查看容器日志"
	@echo "  make shell      - 进入容器shell"
	@echo "  make test       - 测试Nginx配置"
	@echo "  make status     - 查看容器状态"
	@echo "  make deploy     - 构建并部署"
	@echo ""

# 检查Docker是否可用
check-docker:
	@which docker > /dev/null || (echo "错误: Docker未安装" && exit 1)

# 检查必要目录是否存在
check-dirs:
	@test -d $(CONF_DIR) || (echo "错误: $(CONF_DIR) 目录不存在" && exit 1)
	@test -d $(LUA_DIR) || (echo "错误: $(LUA_DIR) 目录不存在" && exit 1)

# 构建Docker镜像
build: check-docker check-dirs
	@echo "构建Docker镜像: $(IMAGE_TAG)"
	docker build -t $(IMAGE_TAG) -t $(LATEST_TAG) -f $(DOCKERFILE) .

# 重新构建镜像（不使用缓存）
rebuild: check-docker check-dirs
	@echo "重新构建Docker镜像（不使用缓存）: $(IMAGE_TAG)"
	docker build --no-cache -t $(IMAGE_TAG) -t $(LATEST_TAG) -f $(DOCKERFILE) .

# 运行容器（前台模式，用于调试）
run: check-docker
	@echo "运行容器（前台模式）..."
	docker run -it --rm \
		--name $(CONTAINER_NAME) \
		-p $(PORT):80 \
		-p $(SSL_PORT):443 \
		-v $(PWD)/$(CONF_DIR)/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf:ro \
		-v $(PWD)/$(CONF_DIR)/conf.d:/etc/nginx/conf.d:ro \
		-v $(PWD)/$(LUA_DIR):/usr/local/openresty/site/lualib/:ro \
		-v $(PWD)/$(HTML_DIR):/usr/local/openresty/nginx/html:ro \
		-v $(PWD)/$(LOGS_DIR):/var/log/nginx \
		$(IMAGE_TAG)

# 启动容器（后台模式）
start: check-docker
	@echo "启动容器（后台模式）..."
	docker run -d \
		--name $(CONTAINER_NAME) \
		-p $(PORT):80 \
		-p $(SSL_PORT):443 \
		-v $(PWD)/$(CONF_DIR)/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf:ro \
		-v $(PWD)/$(CONF_DIR)/conf.d:/etc/nginx/conf.d:ro \
		-v $(PWD)/$(LUA_DIR):/usr/local/openresty/site/lualib/:ro \
		-v $(PWD)/$(HTML_DIR):/usr/local/openresty/nginx/html:ro \
		-v $(PWD)/$(LOGS_DIR):/var/log/nginx \
		--restart unless-stopped \
		$(IMAGE_TAG)
	@echo "容器已启动，访问 http://localhost:$(PORT)"

# 停止容器
stop: check-docker
	@echo "停止容器..."
	-docker stop $(CONTAINER_NAME)

# 重启容器
restart: stop start
	@echo "容器已重启"

# 删除容器
clean: stop
	@echo "删除容器..."
	-docker rm $(CONTAINER_NAME)

# 彻底清理（容器和镜像）
clean-all: clean
	@echo "删除镜像..."
	-docker rmi $(IMAGE_TAG) $(LATEST_TAG)

# 查看容器日志
logs: check-docker
	docker logs -f $(CONTAINER_NAME)

# 进入容器shell
shell: check-docker
	docker exec -it $(CONTAINER_NAME) /bin/sh

# 测试Nginx配置
test: check-docker
	docker exec $(CONTAINER_NAME) nginx -t

# 重载Nginx配置
reload: check-docker
	docker exec $(CONTAINER_NAME) nginx -s reload

# 查看容器状态
status: check-docker
	@echo "容器状态:"
	@docker ps -a | grep $(CONTAINER_NAME) || echo "容器未运行"

# 构建并部署
deploy: build start
	@echo "部署完成!"

# 显示镜像信息
images: check-docker
	@echo "Docker镜像:"
	@docker images | grep $(IMAGE_NAME) || echo "没有找到相关镜像"